var user_ok = false;
var pass_ok = false;
var mail_ok = false;

$(document).ready(function() {
    $('#pass1').focusout(validate_pass1);
    $('#pass1').keyup(validate_pass1);
    $('#pass1').focusin(ocultar_pass1(1));
    
    $('#mail').focusout(validate_mail);
    $('#mail').focusin(ocultar_mail);

    $('#pass2').focusout(validate_pass2);
    $('#pass2').focusin(ocultar_pass2);
    $('#pass1').focusout(validate_pass2);
    $('#pass1').focusin(ocultar_pass2);
    
    $('#avatar').focusout(checkFilled); 
});


function checkFilled() {
    var filled = 0;
    // La funcio comprova que sa emplenat algun dels camps
    var y = document.formReg.pass1.value;
    y = $.trim(y);
    if (y.length > 0 && pass_ok)
        
    var y2 = document.formReg.pass2.value;
    y2 = $.trim(y2);
    if (y2.length > 0 && pass_ok)
        filled++;

    var z = document.formReg.mail.value;
    z = $.trim(z);
    if (z.length > 0 && mail_ok)
        filled++;
    
    var x = document.formReg.avatar.value;
    x = $.trim(x);
    if (x.length > 0)
        filled++;

    if (filled > 0) document.getElementById("submit").disabled = false;        
    else document.getElementById("submit").disabled = true;
}

// Funcions per validar els camps
function validate_mail() {
     var mail = $('#mail').val();
     if (mail.length == 0) {
         $("#mail_error").html("Camp buit");
         $("#mail_error").slideDown();
     }
     else if (mail.length <= 50 &&
         /^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9]+([.][a-zA-Z0-9]+)*[.][a-zA-Z]{2,4}$/.test(mail))
     {
         $("#mail_valid").html("Email Correcto");
         $("#mail_valid").slideDown();
         mail_ok = true;
     }
     else {
        $("#mail_error").html("Email Incorrecto");
        $("#mail_error").slideDown();
        mail_ok = false;
    }
    checkFilled();
}

function ocultar_mail() {
    $("#mail_error").hide();
    $("#mail_valid").hide();
}

function validate_pass1() {
    var pass1 = $('#pass1').val();
    if (pass1.length == 0) {
       $("#password_ferror").html("camp buit");
       $("#password_ferror").slideDown();
       ocultar_pass1(2);
    } 
    else {
        ocultar_pass1(1);
        if (pass1.length > 0 && pass1.length < 8) {
              $("#password_fort").html("Molt dolenta");
              $("#password_fort").slideDown();
        }
        else if (/^[a-zA-Z]{1,20}$/.test(pass1)) {
              $("#password_fort").html("Dolenta");
              $("#password_fort").slideDown();
         }
        else if (/^[a-zA-Z0-9]{1,20}$/.test(pass1)) {
              $("#password_fort").html("Normal");
              $("#password_fort").slideDown();
        }
        else {
              $("#password_fort").html("Buena");
              $("#password_fort").slideDown();
        }
    }
    checkFilled();
}

function ocultar_pass1($num) {
   if ($num == 1) $("#password_ferror").hide();
   else $("#password_fort").hide();
}

function validate_pass2() {
    var pass1 = $('#pass1').val();
    var pass2 = $('#pass2').val();
    if (pass1.length > 0 && pass2.length > 0 && pass1 == pass2) {
        $("#password_valid").html("Els passwords coincideixen");
        $("#password_valid").slideDown();
        pass_ok = true;
    }
    else if (pass1.length > 0 && pass2.length > 0) {
        $("#password_error").html("Els passwords no son iguals");
        $("#password_error").slideDown();
        pass_ok = false;
    }
    checkFilled();
}

function ocultar_pass2() {
   $("#password_error").hide();
   $("#password_valid").hide();
}